let sqlstring = require('sqlstring');
let cnn = require('./dbManager');
let uuid = require('uuid/v4');

module.exports = {
    addCategory: function (info, cb) {
        info.tradeid = uuid();
        let sf = "insert into t_trade set ?";
        let sql = sqlstring.format(sf, info)
        cnn.query(sql, function (err, results) {
            if (err) {
                cb(400)
                console.error(err);
                return;
            }
            cb(200);
        });
    },
    delCategory: function (info, cb) {
        let sf = "delete from t_trade where ??=?";
        let sql = sqlstring.format(sf, info);
        console.log(sql);
        cnn.query(sql, function (err, results) {
            if (err) {
                cb(400);
                console.error(err);
                return;
            }
            cb(200)
            console.log(JSON.stringify(results));
        });
    },
    updateCategory: function (info, cb) {
        let prarams = [info, "tradeid", info['tradeid']];
        let sf = "update t_trade set ? where ??=?"
        let sql = sqlstring.format(sf, prarams);
        cnn.query(sql, function (err, results) {
            if (err) {
                cb(400);
                console.error(err);
                return;
            }
            cb(200)
            console.log(JSON.stringify(results));
        });
    },
    getCategoryList: function (info, cb) {
        let pageSize = info['pageSize'];
        if(pageSize == undefined) pageSize = 20;
        let sql = `select * from t_trade limit ${info.page},${pageSize}`;
        cnn.query(sql, function (err, results) {
            if (err) {
                cb(400);
                console.error(err);
                return;
            }
            cb(200,results);
        });
    },
};